#include "face_cutter_trace_dialog.hpp"

#include <iostream>

#include "scenegraph.hpp"

#include "drop_cutter.hpp"
#include "position_solutions.h"

#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/nodes/SoCoordinate3.h>
#include <Inventor/nodes/SoLineSet.h>
#include <Inventor/nodes/SoBaseColor.h>

#include <QCoreApplication>


SoCoordinate3 *coords;

SoLineSet *tra;

class tr_note : public tracing_note
{public:
  tr_note()
  {
    last=0;
    n=0;
    
  }
  

  void tic(const char* ,int pc, drop_cylinder_cutter_3d &cutter)
  {
    for(long i=last; i< cutter.toolpath.size(); i++)
      {
	const point &p=cutter.toolpath[i];
	
	coords->point.set1Value(n++,p);
	if(i%10==0)
	  {
	    point normal=p+point_dir(p)*4;
	    coords->point.set1Value(n++,normal);
	    coords->point.set1Value(n++,p);
	  }
	
      }
    tra->numVertices.set1Value(0,n);
    last=cutter.toolpath.size();
    // std::cerr<<"\rtraaacing: "<<pc;
    QCoreApplication::processEvents();
  }

  long last;
  long n;

};


drop_cylinder_cutter_3d drop;

face_cutter_trace_dialog::face_cutter_trace_dialog(QWidget *parent):QWidget(parent){
  setupUi(this);
	//	drop.setmodel(model::tris);
}
	 
void face_cutter_trace_dialog::on_buildtracebutton_clicked(){
  double D=tooldiamspin->value();
  double dx=dxspin->value();
  double dy=dyspin->value();
  double safez=safezspin->value();

  static SoSeparator *trace=NULL;
  if(trace==NULL)
    {
      trace=new SoSeparator;
      trace->ref();
    }
  else
    {
      trace->removeAllChildren();
    }		
  coords=new SoCoordinate3;
  tra=new SoLineSet;
  SoBaseColor * tcol = new SoBaseColor;
  tcol->rgb = SbColor(1,1,1);
  trace->addChild(tcol);
  trace->addChild(coords);
  trace->addChild(tra);
  scenegraph::root->addChild(trace);

  
  model::tooldiam=D;
  drop=drop_cylinder_cutter_3d(model::tris,32);

  tr_note note;
  model::path=drop.gen_toolpath(D,dx,dy,safez,&note);
	
  cout<<"---\n";
}
	
